﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DALHoaDon
/// </summary>
public class DALHoaDon : StandardConnectDB, IHoaDon
{
	public DALHoaDon()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    #region IHoaDon Members

    public int insertHD(HoaDon dtoHoaDon)
    {
        SqlCommand sqlCmd = new SqlCommand("insertHD", this.Conn);
        sqlCmd.CommandType = CommandType.StoredProcedure;

        sqlCmd.Parameters.Add("@spNgayGiaoHang",SqlDbType.DateTime);
        sqlCmd.Parameters["@spNgayGiaoHang"].Value = dtoHoaDon.NgayGiaoHang;

        sqlCmd.Parameters.Add("@spNgayDatHang", SqlDbType.DateTime);
        sqlCmd.Parameters["@spNgayDatHang"].Value = dtoHoaDon.NgayDatHang;

        sqlCmd.Parameters.Add("@spTenNguoiNhan", SqlDbType.NVarChar);
        sqlCmd.Parameters["@spTenNguoiNhan"].Value = dtoHoaDon.TenNguoiNhan;

        sqlCmd.Parameters.Add("@spDiaChi", SqlDbType.NVarChar);
        sqlCmd.Parameters["@spDiaChi"].Value = dtoHoaDon.DiaChi;

        sqlCmd.Parameters.Add("@spEmail", SqlDbType.NVarChar);
        sqlCmd.Parameters["@spEmail"].Value = dtoHoaDon.DtoThanhVien.Email;

        sqlCmd.Parameters.Add("@spMaHd", SqlDbType.Int);
        sqlCmd.Parameters["@spMaHd"].Direction = ParameterDirection.Output;

        if (sqlCmd.Connection.State == ConnectionState.Closed)
            sqlCmd.Connection.Open();
        try 
            {
                sqlCmd.ExecuteNonQuery();
                return Convert.ToInt32(sqlCmd.Parameters["@spMaHd"].Value);
            }
            catch (Exception ex)
            {
                sqlCmd.Connection.Close();
               System.Console.WriteLine( ex.Message);
               throw ex;
            }
            finally
            {
                sqlCmd.Connection.Close();
            }
            
    }

    public HoaDon getHDbyID(int id)
    {
        Dictionary<string, object> paramlist = new Dictionary<string, object>();
        paramlist.Add("@mahd",id);
        DataTable dt = this.DTExecuteSelect("getHDbyID", paramlist);

        return this.convertDTtoHoaDon(dt.Rows[0]);
    }

    public HoaDon getHAbyEmail(string email)
    {
        Dictionary<string, object> paramlist = new Dictionary<string, object>();
        paramlist.Add("@spEmail", email);
        DataTable dt = this.DTExecuteSelect("getHAbyEmail", paramlist);

        return this.convertDTtoHoaDon(dt.Rows[0]);
    }

    #endregion

    #region IHoaDon Members


    public HoaDon convertDTtoHoaDon(DataRow dr)
    {
        HoaDon dtoHoaDon = new HoaDon();
        ThanhVien dtoThanhVien = new ThanhVien();
        IThanhVien iThanhVien  = new DALThanhVien();
        if (dr != null)
        {
            dtoHoaDon.MaHD = int.Parse(dr["MAHD"].ToString());
            dtoHoaDon.NgayDatHang = DateTime.Parse(dr["NGDATHANG"].ToString());
            dtoHoaDon.NgayGiaoHang = DateTime.Parse(dr["NGGIAOHANG"].ToString());
            dtoHoaDon.TenNguoiNhan = dr["TENNGUOINHAN"].ToString();
            dtoHoaDon.DiaChi = dr["DIACHIGH"].ToString();
            dtoHoaDon.DtoThanhVien = iThanhVien.SelectByEmail(dr["EMAIL"].ToString());

            return dtoHoaDon;
        }
        return dtoHoaDon;
    }

    #endregion
}
